package org.abettor.octopus.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 用于发现数据库驱动程序的类
 * @author abettor
 *
 */
public class DriverFinder
{
	private static final String FILE = "./conf/driver.xml";

	private static Object synLocker = new Object();
	private static DriverFinder instance = null;
	private Document doc;

	/**
	 * 根据驱动程序连接器的名称获取数据库驱动程序的类名
	 * @param connectorName 用于连接数据库的标识名称
	 * @return 成功返回数据库驱动程序的类名，否则返回null
	 */
	public static String getDriverName(String connectorName)
	{
		DriverFinder df = null;
		try
		{
			df = getInstance();
		}
		catch (DocumentException e)
		{
			e.printStackTrace();
			return null;
		}

		Element root = df.doc.getRootElement();
		List connectorNodes = root.elements("Connector");
		if(connectorNodes == null)
		{
			return null;
		}

		Iterator it = connectorNodes.iterator();
		while(it.hasNext())
		{
			Element node = (Element) it.next();
			if(connectorName.equals(node.attributeValue("name")))
			{
				Element clazz = node.element("Class");
				if(clazz == null)
				{
					return null;
				}

				return clazz.getTextTrim();
			}
		}

		return null;
	}

	/**
	 * 根据驱动程序连接器的名称获取数据库连接字符串的格式
	 * @param connectorName 用于连接数据库的标识名称
	 * @return 成功返回数据库连接字符串例子，否则返回null
	 */
	public static String getConnectionString(String connectorName)
	{
		DriverFinder df = null;
		try
		{
			df = getInstance();
		}
		catch (DocumentException e)
		{
			e.printStackTrace();
			return null;
		}

		Element root = df.doc.getRootElement();
		List connectorNodes = root.elements("Connector");
		if(connectorNodes == null)
		{
			return null;
		}

		Iterator it = connectorNodes.iterator();
		while(it.hasNext())
		{
			Element node = (Element) it.next();
			if(connectorName.equals(node.attributeValue("name")))
			{
				Element url = node.element("URL");
				if(url == null)
				{
					return null;
				}

				return url.getTextTrim();
			}
		}

		return null;
	}

	/**
	 * 获取所有驱动程序连接器的名称列表
	 * @return 成功返回用于连接数据库的标识名称列表，否则返回null
	 */
	public static List<String> getConnectors()
	{
		DriverFinder df = null;
		try
		{
			df = getInstance();
		}
		catch (DocumentException e)
		{
			e.printStackTrace();
			return null;
		}

		Element root = df.doc.getRootElement();
		List connectorNodes = root.elements("Connector");
		if(connectorNodes == null)
		{
			return null;
		}

		List<String> connList = new ArrayList<String>();
		Iterator it = connectorNodes.iterator();
		while(it.hasNext())
		{
			Element node = (Element) it.next();
			connList.add(node.attributeValue("name"));
		}

		return connList;
	}

	//	私有默认构造函数
	private DriverFinder() throws DocumentException
	{
		SAXReader reader = new SAXReader();
		doc = reader.read(new File(FILE));
	}

	//	获取singleton模式的当前类的实例
	private static DriverFinder getInstance() throws DocumentException
	{
		synchronized(synLocker)
		{
			if(instance == null)
			{
				instance = new DriverFinder();
			}
		}

		return instance;
	}
}
